<?php

/**
 * 验证码
 * Class yzm
 */
class yzm {

    public function  __construct() {
        $this->_session_start();
    }

    private function _session_start() {
        $session_storage = 'session_' . pc_base::load_config('system', 'session_storage');
        pc_base::load_sys_class($session_storage);
    }

    //看下session
    public function see() {
        echo '<pre>';
        print_r($_SESSION);
    }

    //次数限制
    private function checktime($type, $m) {
        if (isset($_SESSION['yzm'][$type][$m]['extime'])) {
            if (time() - $_SESSION['yzm'][$type][$m]['extime'] < 60) {
                json_exit(1, 300, '1分钟内无需重复发送短信验证码');
            }
        } else {
            $_SESSION['yzm'][$type][$m]['extime'] = time();
        }
    }

    //获取随机验证码
    private function setcode($type) {
        $verify = rand(100000, 999999);
        $_SESSION['yzm'][$type]['verifyt'] = $verify;

        return $verify;
    }

    //发邮箱
    public function email() {
        pc_base::load_sys_func('mail');

        if (isset($_POST['email']) && is_email($_POST['email'])) {
            $email = $_POST['email'];
        } else {
            json_exit(1, 300, '请提供正确的邮箱');
        }

        $this->checktime('email', $email);

        $verify = $this->setcode('email');

        //邮件内容
        $subject = '感谢您的注册';
        $message = '您的邮箱验证码:' . $verify;

        $email_config = getcache('common', 'commons');

        $mail = [
            'mailsend' => 2,
            'maildelimiter' => 1,
            'mailusername' => 1,
            'server' => $email_config['mail_server'],
            'port' => intval($email_config['mail_port']),
            'mail_type' => intval($email_config['mail_type']),
            'auth' => intval($email_config['mail_auth']),
            'from' => $email_config['mail_from'],
            'auth_username' => $email_config['mail_user'],
            'auth_password' => $email_config['mail_password']
        ];

        if (sendmail($email, $subject, $message, $email_config['mail_from'], $mail)) {
            json_exit(1, 100, '验证码已发送到您的邮箱');
        } else {
            json_exit(1, 300, '发送失败请重试');
        }
    }

    //发短信
    public function mobile() {

        if (isset($_POST['mobile']) && preg_match("/^1\d{10}$/", $_POST['mobile'])) {
            $mobile = $_POST['mobile'];
        } else {
            json_exit(1, 300, '请提供正确的手机号码');
        }

        if (isset($_POST['type']) && $_POST['type'] == 'reg') {
            $db_member = pc_base::load_model('member_model');
            $res = $db_member->get_one("username=$mobile");
            if ($res) {
                json_exit(1, 300, '手机号已被注册');
            }
        }

        $this->checktime('mobile', $mobile);
        $db_mobile_yzm = pc_base::load_model('mobile_yzm_model');
        $lasttime = time() - 60;
        $num = $db_mobile_yzm->count("ip='" . $_SERVER["REMOTE_ADDR"] . "' and lasttime>" . $lasttime);
        if ($num == 0) {
            $num = $db_mobile_yzm->count("ip='" . $_SERVER["REMOTE_ADDR"] . "' and lasttime>" . $lasttime);
            $res = $db_mobile_yzm->insert([
                'ip' => $_SERVER["REMOTE_ADDR"],
                'lasttime' => time(),
                'mobile' => $mobile,
            ]);
            $verify = $this->setcode('mobile');

            $params = [
                'name' => 'dxwgjxs',     //必填参数。用户账号 123456789a
                'pwd' => '85DAB3A663119F52C3B4BDC24478',     //必填参数。（web平台：基本资料中的接口密码）
                'content' => '短信验证码为：' . $verify . '，请勿将验证码提供给他人。',   //必填参数。发送内容（1-500 个汉字）UTF-8编码
                'mobile' => $mobile,   //必填参数。手机号码。多个以英文逗号隔开
                'stime' => '',   //可选参数。发送时间，填写时已填写的时间发送，不填时为当前时间发送
                'sign' => '高见学社',    //必填参数。用户签名。
                'type' => 'pt',  //必填参数。固定值 pt
                'extno' => ''    //可选参数，扩展码，用户定义扩展码，只能为数字
            ];

            $params = http_build_query($params);

            $url = "http://web.duanxinwang.cc/asmx/smsservice.aspx?" . $params; //提交的url地址

            $res = file_get_contents($url);  //获取信息发送后的状态

            $res = explode(',', $res);

            if ($res[0] == '0') {
                json_exit(1, 100, '验证码已发送到您的手机');
            } else {
                json_exit(1, 300, $res);
            }
        }

    }

}